package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.UserLeaveDao;
import com.rk.kbk.dao.UserDao;
import com.rk.kbk.model.UserLeave;
import com.rk.kbk.model.User;
import com.rk.kbk.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by bilibili on 2017/3/18.
 */

@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/leave")
public class LeaveController {
    private Logger log = Logger.getLogger(this.getClass());


    @Autowired
    private UserLeaveDao userLeaveDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;




    /**
     * @骑手请假
     * @param dateStr
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/saveLeaveDate",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp saveLeaveDate(String dateStr,Long rideId) {
        log.debug("骑手请假");
        User user = userDao.findOne(rideId);
        String dateArr[] = dateStr.split("\\*\\*\\*");
        List<Date> leaveDateList = new ArrayList<>();
        List<UserLeave> saveUserLeaveList = new ArrayList<>();
        for (int i = 0; i < dateArr.length; i++) {
            UserLeave userLeave = new UserLeave();
            userLeave.setUser(user);
            userLeave.setApprove(1);//默认批准
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(dateArr[i]);
                userLeave.setLeaveDate(date);
                saveUserLeaveList.add(userLeave);
                leaveDateList.add(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        /****************************满足一下两个条件请假成功********************************/
        List<UserLeave> userLeaveList = userLeaveDao.findByLeaveDateInAndUser(leaveDateList,user);//根据请假日期查看一下 是否已经该日期已经是请假日期
        if (!userLeaveList.isEmpty()) {
            return JsonResp.ok("请假日期出现重复！");
        }else if(leaveDateList.size()>user.getRestDays()){
             return JsonResp.ok("请假天数超出允许请假天数！");
         }else {
            userLeaveDao.save(saveUserLeaveList);
        }
        return JsonResp.ok();
    }




    /**
     * @获取骑手可供请假天数
     * @param rideId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/getTotalLeaveDays",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp getTotalLeaveDays(Long rideId) {
        log.debug("获取骑手可供请假天数");
        User user = userDao.findOne(rideId);
        return JsonResp.ok(user.getRestDays());
    }




}
